//
//  AppDelegate.m
//  MooMusic
//
//  Created by wjy on 15/10/19.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "AppDelegate.h"
#import "WLG_HomePageViewController.h"
// 导入shareSDK头文件
#import <ShareSDK/ShareSDK.h>
// 腾讯头文件
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>

// 导入引导页的头文件
#import "LTY_GuidePage.h"
// 导入ScrollViewController头文件
#import "ScrollViewController.h"

#define shareAppKey @"b7fe5c3bf8f4"

@interface AppDelegate ()<UIScrollViewDelegate>

@property (nonatomic, strong) LTY_GuidePage *guidePage;  /**< 引导页*/
@property (nonatomic, strong)ScrollViewController *scVC;

@end

@implementation AppDelegate

// 初始化消息通知
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(receiveNotifica:)
                       name:@"heiyejianglin" object:nil];
        
        NSNotificationCenter *center2 = [NSNotificationCenter defaultCenter];
        [center2 addObserver:self selector:@selector(receiveNotifica2:) name:@"limingjianglin" object:nil];
        
    }
    return self;
}

- (void)receiveNotifica:(NSNotification *)center
{
        self.nigView = [[UIView alloc] initWithFrame:self.window.bounds];
        self.nigView.userInteractionEnabled = NO;
        self.nigView.hidden = NO;
        self.nigView.backgroundColor = [UIColor blackColor];
        self.nigView.alpha = 0.6;
        [self.window addSubview:self.nigView];
}

- (void)receiveNotifica2:(NSNotification *)center
{
    self.nigView.userInteractionEnabled = NO;
    self.nigView.hidden = YES;
    self.nigView.backgroundColor = [UIColor blackColor];
    
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // shareSDk:
    // shareSDK appKey : b7fe5c3bf8f4
    // App Secret: aa27d60d1a5ecfec756af3b70889f14f
    
    
    // QQ腾讯分享 :
    // APP ID: 1104926768
    // 十六进制:QQ41DBD830
    // tencent1104926768
    // APP KEY: 6Q9Sm7VV14hvLZHq
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
   
    
    WLG_HomePageViewController *homePage = [[WLG_HomePageViewController alloc] init];
    UINavigationController *nVG = [[UINavigationController alloc] initWithRootViewController:homePage];
    self.tabBarSingle = [TabBarController shareTabBar];
    self.tabBarSingle.viewControllers = @[nVG];

    // 把抽屉设置成跟视图
    LeftViewController *leftvc = [[LeftViewController alloc]init];
    self.SideMenuController = [[RESideMenu alloc]initWithContentViewController:self.tabBarSingle leftMenuViewController:leftvc rightMenuViewController:nil];
    self.SideMenuController.menuPreferredStatusBarStyle = 1;
    self.SideMenuController.delegate = self;
    self.SideMenuController.contentViewShadowColor = [UIColor yellowColor];
    self.SideMenuController.contentViewShadowOffset = CGSizeMake(0, 0);
    self.SideMenuController.contentViewShadowOpacity = 0.6;
    self.SideMenuController.contentViewShadowRadius = 12;
    self.SideMenuController.contentViewShadowEnabled = YES;
    // 把抽屉的手势给关了 就不会滑动了
    self.SideMenuController.panGestureEnabled = NO;
    // 设置根视图控制器
    
    self.window.rootViewController = self.SideMenuController;
    
    
    
    [ShareSDK registerApp:shareAppKey];//字符串api20为您的ShareSDK的AppKey
    
    //添加QQ空间应用  注册网址  http://connect.qq.com/intro/login/
    [ShareSDK connectQZoneWithAppKey:@"1104926768"
                           appSecret:@"6Q9Sm7VV14hvLZHq"
                   qqApiInterfaceCls:[QQApiInterface class]
                     tencentOAuthCls:[TencentOAuth class]];
    
    /* 初始化数据库管理的三方 */
    /* 给MagicalRecord初始化 */
    [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"MyDatabase.sqlite"];
    
    
    // 获取沙盒版本号
    NSString *homeDirectoryVersionString = [LTY_GuidePage getHomeDirectoryVersionNumber];
    // 获取当前版本号
    NSString *atPresentVersionString = [LTY_GuidePage getAtPresentVersionNumber];
    // 若沙盒版本号与当前版本号一致, 尚未更新
    if ([homeDirectoryVersionString isEqualToString:atPresentVersionString]) {
        NSLog(@"未更新 --- 显示主页");
        
        [self createHomePage];
        
    } else {
        
        self.scVC = [[ScrollViewController alloc] init];
        self.window.rootViewController = self.scVC;
        
        NSLog(@"更新 --- 显示引导页");
        // 存储当前版本号
        [LTY_GuidePage saveAtPresentVersionNumber:atPresentVersionString];
        
        self.guidePage = [[LTY_GuidePage alloc] initWithFrame:self.window.bounds];
        self.guidePage.userInteractionEnabled = YES;
        self.guidePage.scrollView.delegate = self;
        [self.guidePage.enterButton addTarget:self action:@selector(enterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scVC.view addSubview:_guidePage];
    }

    
    
     [self.window makeKeyAndVisible];
    
    return YES;
}

#pragma mark - 创建首页
- (void)createHomePage
{
    // 如果存在value 就把根视图给windows
    self.window.rootViewController = self.SideMenuController;
    
}

#pragma mark - Button点击事件, 移除引导页并显示主视图控制器
- (void)enterButtonAction:(UIButton *)buttonSender
{
    NSLog(@"跳过");
    [self.guidePage removeFromSuperview];
    [self createHomePage];
}

#pragma mark - 滚动至最后一张图片, 移除引导页并显示主视图控制器
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.guidePage.scrollView.contentOffset.x > self.window.frame.size.width * 2 + self.window.frame.size.width / 3) {
        [self.guidePage removeFromSuperview];
        [self createHomePage];
    }
}

#pragma mark - 滚动视图改变, 分页点随之改变
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.guidePage.pageControl.currentPage = self.guidePage.scrollView.contentOffset.x / (self.guidePage.scrollView.frame.size.width);
}




- (BOOL)application: (UIApplication *)application  handleOpenURL: (NSURL *)url
{
    return [ShareSDK handleOpenURL:url wxDelegate:self];
}

- (BOOL)application: (UIApplication *)application
            openURL: (NSURL *)url
  sourceApplication: (NSString *)sourceApplication
         annotation: (id)annotation
{
    return [ShareSDK handleOpenURL: url
                 sourceApplication:sourceApplication
                        annotation: annotation
                        wxDelegate: self];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
